package cn.wolfcode.shop.server.service.impl;

import cn.wolfcode.shop.api.domain.Product;
import cn.wolfcode.shop.api.domain.ProductCatalogPropertyValue;
import cn.wolfcode.shop.api.domain.ProductDesc;
import cn.wolfcode.shop.api.domain.ProductImage;
import cn.wolfcode.shop.api.page.PageResult;
import cn.wolfcode.shop.api.query.ProductListQueryObject;
import cn.wolfcode.shop.api.query.ProductQueryObject;
import cn.wolfcode.shop.api.service.IProductService;
import cn.wolfcode.shop.api.vo.ProductVo;
import cn.wolfcode.shop.server.mapper.*;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDescMapper productDescMapper;

    @Autowired
    private ProductImageMapper productImageMapper;

    @Autowired
    private CatalogPropertyMapper catalogPropertyMapper;

    @Autowired
    private ProductCatalogPropertyValueMapper productCatalogPropertyValueMapper;

    @Override
    public List<Product> getAll() {
        return productMapper.selectAll();
    }

    @Override
    public List<Product> listProducts() {
        return productMapper.listProducts();
    }

    @Override
    public Product get(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null) {
            throw new RuntimeException("该商品不存在");
        }
        return product;
    }

    @Override
    public Product getProductInfoVo(Long id) {
        Product product = productMapper.selectProductInfoVoByPrimaryKey(id);
        if (product == null) {
            throw new RuntimeException("该商品不存在");
        }
        return product;
    }

    @Override
    public int save(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public Product saveProduct(ProductVo vo) {
        Product product = vo.getProduct();
        product.setCreatedDate(new Date());
        product.setLastModifiedDate(new Date());
        productMapper.insert(product);

        ProductDesc productDesc = vo.getProductDesc();
        productDesc.setProductId(product.getId());
        productDescMapper.insert(productDesc);

        List<ProductImage> productImages = vo.getProductImages();

        for (ProductImage productImage : productImages) {
            if (productImage.getImagePath() != null && !"".equals(productImage.getImagePath())) {
                productImage.setProductId(product.getId());
                productImageMapper.insert(productImage);
            }
        }

        List<ProductCatalogPropertyValue> propertys = vo.getPropertys();
        if (propertys != null) {
            for (ProductCatalogPropertyValue property : propertys) {
                property.setCatalogPropertyId(product.getId());
                productCatalogPropertyValueMapper.insert(property);
            }
        }
        return product;
    }

    @Override
    public int update(Product product) {
        return productMapper.updateByPrimaryKey(product);
    }

    @Override
    public List<ProductCatalogPropertyValue> selectProductCatalogPropertyValueByProductId(Long id) {
        return productCatalogPropertyValueMapper.selectByProductId(id);
    }

    @Override
    public PageResult query(ProductQueryObject qo) {
        int rows = productMapper.queryForCount(qo);
        if (rows == 0) {
            return new PageResult(0, Collections.EMPTY_LIST, 1, 1);
        }
        List<?> data = productMapper.queryForList(qo);
        return new PageResult(rows, data, qo.getCurrentPage(),qo.getPageSize());
    }


    @Override
    public PageResult queryProductListInterface(ProductListQueryObject qo) {

        if (qo.getSortType() == 1) {
            qo.setOrderByCondition("order by market_price desc");
        } else if (qo.getSortType() == 2) {
            qo.setOrderByCondition("order by market_price asc");
        } else {
            qo.setOrderByCondition(null);
        }

        int rows = productMapper.queryForCountInterface(qo);
        if (rows == 0) {
            return new PageResult(0, Collections.EMPTY_LIST, 1, 1);
        }
        List<?> data = productMapper.queryForListInterface(qo);
        return new PageResult(rows, data, qo.getCurrentPage(), qo.getPageSize());
    }


}
